<?php

declare(strict_types=1);

namespace app\admin\controller;

use app\admin\controller\Base;
use think\Request;

/**
 * 物流公司控制器
 */
class LogisticsCompany extends Base
{
    protected $request;
    protected $service;
    protected $validate;
    protected $operator_id;
    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->service = new \app\admin\service\LogisticsCompany();
        $this->validate = new \app\admin\validate\LogisticsCompany();

        $this->operator_id = $request->operator_id;
    }

    /**
     * @describe 获取物流公司列表
     * @author: Bobo
     * @date: 2024-08-16 03:04:33
     * @param [] type
     * @return Renderable
     */
    public function index()
    {
        $params = $this->request->params;

        if (!$this->validate->scene('search')->check($params)) {
            return $this->returnJsonData(100001, [], $this->validate->getError());
        }

        $pageIndex  = isset($params['pageIndex'])   ? intval($params['pageIndex']) : 1;
        $pageSize   = isset($params['pageSize'])    ? intval($params['pageSize']) : 10;
        $name       = isset($params['name'])        ? trim($params['name']) : null;
        $status     = isset($params['status'])      ? intval($params['status']) : null;

        $where = ['is_del' => 0];
        if ($status > -1) {
            $where['status'] = $status;
        }
        if ($name) {
            $where[] = ['name', 'like', "%{$name}%"];
        }

        $res = $this->service->getList($where, $pageIndex, $pageSize);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * @describe 获取物流公司详情
     * @author: Bobo
     * @date: 2024-08-16 03:07:37
     * @param [int] $id 物流公司ID @required
     * @return Renderable
     */
    public function show()
    {
        $params = $this->request->params;
        if (!$this->validate->scene('show')->check($params)) {
            return $this->returnJsonData(100001, [], $this->validate->getError());
        }
        $res  = $this->service->getOneById($params['id']);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * @describe 创建物流公司
     * @author: Bobo
     * @date: 2024-08-16 03:09:17
     * @param [array] $data 创建物流公司的数据 @required
     * @return Renderable
     */
    public function create()
    {
        $data = $this->request->params;
        $data['creator'] = $this->operator_id;

        if (!$this->validate->scene('create')->check($data)) {
            return $this->returnJsonData(100001, [], $this->validate->getError());
        }

        $result = $this->service->create($data);
        $code = $result['code'];
        $data = $result['data'] ?? [];
        $message = $result['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * 更新物流公司
     * @describe 更新物流公司
     * @author: Bobo
     * @date: 2024-08-16 03:11:17
     * @param [int] $id 物流公司ID @required
     * @param [array] $data 更新物流公司的数据 @required
     * @return Renderable
     */
    public function update()
    {
        $params = $this->request->params;

        if (!$this->validate->scene('update')->check($params)) {
            return $this->returnJsonData(100001, [], $this->validate->getError());
        }

        $params['updater'] = $this->operator_id;
        $params['update_time'] = time();

        $result = $this->service->update($params);
        $code = $result['code'];
        $data = $result['data'] ?? [];
        $message = $result['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * 删除物流公司
     * @param int $id 物流公司ID
     */
    /**
     * @describe 删除物流公司
     * @author: Bobo
     * @date: 2024-08-16 03:13:56
     * @param [int] $id 物流公司ID @required
     * @return Renderable
     */
    public function delete()
    {
        $params = $this->request->params;
        if (!$this->validate->scene('delete')->check($params)) {
            return $this->returnJsonData(100001, [], $this->validate->getError());
        }
        $ids =  trim($params['ids']);
        $res = $this->service->batchLogicalDelete($ids);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * @describe 批量修改状态
     * @author: Bobo
     * @date: 2024-08-16 03:13:56
     * @param array ids 物流公司ID数组
     * @param int status 状态值 0-关闭 1-开启
     * @return Renderable
     */
    public function batchUpdateStatus()
    {
        $params = $this->request->params;
        if (!isset($params['ids']) || !isset($params['status'])) {
            return $this->returnJsonData(100001, [], '参数错误');
        }

        $ids = is_array($params['ids']) ? $params['ids'] : explode(',', $params['ids']);
        $status = intval($params['status']);

        $res = $this->service->batchUpdateStatus($ids, $status);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * @describe 
     * @author: Bobo
     * @date: 2024-08-16 19:07:39
     * @param [type] @required
     * @return Renderable
     */
    public function getFieldEnum()
    {
        $params = $this->request->params;
        $res = $this->service->getFieldEnum($params['field']);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    public function getSelect()
    {
        $arrData = $this->service->getSelect();
        return json($arrData);
    }
}
